import {defHttp} from '/@/utils/http/axios';
import { useMessage } from "/@/hooks/web/useMessage";

const { createConfirm } = useMessage();

enum Api {
  list = '/afterSale/afterSales/list',
  save= '/afterSale/afterSales/add',
  edit= '/afterSale/afterSales/edit',
  deleteOne = '/afterSale/afterSales/delete',
  deleteBatch = '/afterSale/afterSales/deleteBatch',
  importExcel = '/afterSale/afterSales/importExcel',
  exportXls = '/afterSale/afterSales/exportXls',
  afterSaleMembersList = '/afterSale/afterSales/listAfterSaleMembersByMainId',
  afterSaleMembersSave= '/afterSale/afterSales/addAfterSaleMembers',
  afterSaleMembersEdit= '/afterSale/afterSales/editAfterSaleMembers',
  afterSaleMembersDelete = '/afterSale/afterSales/deleteAfterSaleMembers',
  afterSaleMembersDeleteBatch = '/afterSale/afterSales/deleteBatchAfterSaleMembers',
  // 售后反馈相关接口
  afterSalesRecordQuery = '/afterSales/afterSaleRecord/queryByParentId',
  afterSalesRecordAdd = '/afterSales/afterSaleRecord/add',
  afterSalesRecordEdit = '/afterSales/afterSaleRecord/edit',
  afterSalesRecordDelete = '/afterSales/afterSaleRecord/delete',
  afterSalesRecordDeleteBatch = '/afterSales/afterSaleRecord/deleteBatch',
  // 进入售后记录表接口
  enterServiceAfterSaleLogs = '/afterSale/afterSales/enterServiceAfterSaleLogs',
  // 客户续费相关接口
  getCustomerNameAndId = '/customers_pool/customersPool/getCustomerNameAndId',
  getProductTypes = '/products/productType/getProductTypeNameAndId',
  getProductName = '/products/productType/getProductNameByProductTypeId',
  backContracts = '/afterSale/afterSales/renewServiceToContract',
}
/**
 * 导出api
 * @param params
 */
export const getExportUrl = Api.exportXls;

/**
 * 导入api
 */
export const getImportUrl = Api.importExcel;

/**
 * 列表接口
 * @param params
 */
export const list = (params) =>
  defHttp.get({ url: Api.list, params });

/**
 * 删除单个
 */
export const deleteOne = (params,handleSuccess) => {
  return defHttp.delete({ url: Api.deleteOne, params }, { joinParamsToUrl: true }).then(() => {
    handleSuccess();
  });
}

/**
 * 批量删除
 * @param params
 */
export const batchDelete = (params, handleSuccess) => {
  createConfirm({
    iconType: 'warning',
    title: '确认删除',
    content: '是否删除选中数据',
    okText: '确认',
    cancelText: '取消',
    onOk: () => {
      return defHttp.delete({ url: Api.deleteBatch, data: params }, { joinParamsToUrl: true }).then(() => {
        handleSuccess();
      });
    }
  });
}

/**
 * 保存或者更新
 * @param params
 */
export const saveOrUpdate = (params, isUpdate) => {
  let url = isUpdate ? Api.edit : Api.save;
  return defHttp.post({ url: url, params },{ isTransformResponse: false });
}
  
/**
 * 列表接口
 * @param params
 */
export const afterSaleMembersList = (params) => {
  if(params['afterSaleId']){
    return defHttp.get({ url: Api.afterSaleMembersList, params });
  }
  return Promise.resolve({});
}

/**
 * 删除单个
 */
export const afterSaleMembersDelete = (params,handleSuccess) => {
  return defHttp.delete({ url: Api.afterSaleMembersDelete, params }, { joinParamsToUrl: true }).then(() => {
    handleSuccess();
  });
}

/**
 * 批量删除
 * @param params
 */
export const afterSaleMembersDeleteBatch = (params, handleSuccess) => {
  createConfirm({
    iconType: 'warning',
    title: '确认删除',
    content: '是否删除选中数据',
    okText: '确认',
    cancelText: '取消',
    onOk: () => {
      return defHttp.delete({ url: Api.afterSaleMembersDeleteBatch, data: params }, { joinParamsToUrl: true }).then(() => {
        handleSuccess();
      });
    }
  });
}

/**
 * 保存或者更新
 * @param params
 */
export const  afterSaleMembersSaveOrUpdate = (params, isUpdate) => {
  let url = isUpdate ? Api.afterSaleMembersEdit : Api.afterSaleMembersSave;
  return defHttp.post({ url: url, params },{ isTransformResponse: false });
}

/**
 * 导入
 */
export const afterSaleMembersImportUrl = '/afterSale/afterSales/importAfterSaleMembers'

/**
 * 导出
 */
export const afterSaleMembersExportXlsUrl = '/afterSale/afterSales/exportAfterSaleMembers'

/**
 * 售后反馈相关API
 */

/**
 * 根据父ID查询售后反馈记录
 * @param params
 */
export const afterSalesRecordQuery = (params) => {
  return defHttp.get({ url: Api.afterSalesRecordQuery, params });
}

/**
 * 新增售后反馈记录
 * @param params
 */
export const afterSalesRecordAdd = (params) => {
  return defHttp.post({ url: Api.afterSalesRecordAdd, params }, { isTransformResponse: false });
}

/**
 * 编辑售后反馈记录
 * @param params
 */
export const afterSalesRecordEdit = (params) => {
  return defHttp.post({ url: Api.afterSalesRecordEdit, params }, { isTransformResponse: false });
}

/**
 * 删除售后反馈记录
 * @param params
 * @param handleSuccess
 */
export const afterSalesRecordDelete = (params, handleSuccess) => {
  return defHttp.delete({ url: Api.afterSalesRecordDelete, params }, { joinParamsToUrl: true }).then(() => {
    handleSuccess();
  });
}

/**
 * 批量删除售后反馈记录
 * @param params
 * @param handleSuccess
 */
export const afterSalesRecordDeleteBatch = (params, handleSuccess) => {
  createConfirm({
    iconType: 'warning',
    title: '确认删除',
    content: '是否删除选中的售后反馈记录',
    okText: '确认',
    cancelText: '取消',
    onOk: () => {
      return defHttp.delete({ url: Api.afterSalesRecordDeleteBatch, data: params }, { joinParamsToUrl: true }).then(() => {
        handleSuccess();
      });
    }
  });
}

/**
 * 进入售后记录表
 * @param params
 */
export const enterServiceAfterSaleLogs = (params) => {
  return defHttp.post({ url: Api.enterServiceAfterSaleLogs, params }, { isTransformResponse: false });
}

/**
 * 获取客户名称和ID列表
 */
export const getCustomerNameAndId = () => {
  return defHttp.get({ url: Api.getCustomerNameAndId });
}

/**
 * 获取产品类别列表
 */
export const getProductTypes = () => {
  return defHttp.get({ url: Api.getProductTypes });
}

/**
 * 根据产品类别ID获取产品名称列表
 */
export const getProductNameByTypeId = (productTypeId) => {
  return defHttp.get({ url: Api.getProductName, params: { productTypeId } });
}

/**
 * 客户续费接口
 * @param params
 */
export const backContracts = (params) => {
  return defHttp.post({ url: Api.backContracts, params }, { isTransformResponse: false });
}
